/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.swosh.stomp.web;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import cz.swosh.stomp.ejb.Person;
import cz.swosh.stomp.ejb.PersonDAO;
import cz.swosh.stomp.ejb.XMLWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import org.w3c.dom.*;

/**
 * Tento servlet se stará o zobrazování a chování objektů na mapě.
 * @author swosh
 */
public class PersonServlet extends HttpServlet {

    @EJB
    PersonDAO personDAO;
    @EJB
    XMLWriter xmlWriter;

    /**
     * V metodě se nejprve zjišťuje jakou hodnotu má parametr action.
     * Pokud má hodnotu setMarkers, tak odešle xml dokument obsahující
     * údaje o souřadnicích objektů na mapě. Pokud parametr action má hodnotu
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String action = request.getParameter("action");
        XMLSerializer serializer = new XMLSerializer();

        if ("clickOnMarker".equals(action)) {
            response.setContentType("text/html;charset=UTF-8");
            int id = Integer.parseInt(request.getParameter("id"));
            Person p = personDAO.getPerson(id);
            PrintWriter out = response.getWriter();
            StringBuilder sb = new StringBuilder();

            sb.append("<div class='info_win'>");
            sb.append("<h3>");
            sb.append(StringEscapeUtils.escapeHtml(p.getName()));
            sb.append("</h3>");
            sb.append("<img class='img_info_win' src='photo/p");
            sb.append(p.getId());
            sb.append("'></img>");
            sb.append("<p> Adresa: ");
            sb.append(StringEscapeUtils.escapeHtml(p.getMunicipalty())).append(", ");
            sb.append(StringEscapeUtils.escapeHtml(p.getStreet())).append(" ");
            sb.append(p.getNumOri());
            sb.append("</p>");
            sb.append("<p> ");
            sb.append(p.getDescription());
            sb.append("</p>");
            sb.append("</div>");

            out.print(sb.toString());
            out.close();
        } else if ("setMarkers".equals(action)) {

            response.setContentType("text/xml;charset=UTF-8");
            List<Person> personList = personDAO.getPersonsLatLonNameId();

        if (personList != null) {
                Document doc = xmlWriter.writeMarkers(personList);

                serializer.setOutputByteStream(response.getOutputStream());
                serializer.serialize(doc);
            } else {
                return;
            }

        }
        }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /**
         * Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet


        (HttpServletRequest request, HttpServletResponse response)
        throws ServletException,
        IOException
        {
            processRequest(request, response);


        }
        /**
         * Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost


        (HttpServletRequest request, HttpServletResponse response)
        throws ServletException,
        IOException
        {
            processRequest(request, response);


        }
        /**
         * Returns a short description of the servlet.
         * @return a String containing servlet description
         */
        @Override
        public String getServletInfo

             () {
        return "Short description";

        }// </editor-fold>
    }
